<template>
<!--  绑定ref-->
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";

export default {
  name: "Scroll",
  data(){
    return {
      scroll: null,
      message: 'hhh'
    }
  },
  props:{
    //传入是否监听滚动
    probeType: {
      type: Number ,
      default: 0
    },
    //监听上拉加载
    pullUpLoad:{
      type: Boolean,
      default: false
    }
  },
  mounted() {
    //创建Scroll对象
    this.scroll = new BScroll(this.$refs.wrapper, {
      click: true,
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad

    })
    //监听窗口滚动
    if ( this.probeType === 2 || this.probeType === 3 ){
      this.scroll.on('scroll', (position)=> {
        // console.log(position);
        //利用emit传出自定义函数和参数position
        this.$emit('scroll', position)
      })

    }

    // console.log(this.scroll);

    //监听上拉加载
    if(this.pullUpLoad){
      this.scroll.on('pullingUp',()=>{
        // console.log('上拉加载');
        this.$emit('pullingUp')
      })

    }

  },
  methods: {
    scrollTo(x, y, time = 600 ){
      //es6语法，使time参数默认值为300
      //封装一个返回顶部的组件函数
      this.scroll && this.scroll.scrollTo( x, y, time )
      // console.log('111');

    },
    //封装refresh刷新
    refresh(){
      // console.log('调用Scroll里refresh的次数');
      this.scroll && this.scroll.refresh()
    },

    //上拉加载更多
    finishPullUp(){
      this.scroll.finishPullUp()
    }

  }
}
</script>

<style scoped>

</style>
